package com.ruoyi.order.station.handle;

import com.github.s7connector.api.S7Connector;
import com.github.s7connector.api.factory.S7ConnectorFactory;
import com.github.s7connector.exception.S7Exception;
import com.github.s7connector.impl.serializer.S7SerializerImpl;
import com.ruoyi.common.utils.processor.FloatPrecisionProcessor;
import com.ruoyi.order.domain.MesDetectNode;
import com.ruoyi.order.station.cmd.DB5400;
import com.ruoyi.order.station.cmd.DB5404;
import com.ruoyi.order.station.cmd.DB5411;
import com.ruoyi.order.station.operate.DB5450;
import com.ruoyi.order.station.operate.DB5452;
import com.ruoyi.order.station.operate.DB5850;
import com.ruoyi.order.station.qrcode.DB5460PLC;
import com.ruoyi.order.station.qrcode.response.DB5463;
import com.ruoyi.order.station.request.*;
import com.ruoyi.order.station.request.instance.*;
import com.ruoyi.order.station.response.DB5410Server;
import com.ruoyi.order.station.response.DB5439Server;
import com.ruoyi.order.station.response.PassResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
@Slf4j
@Service
public class StationExchangeService {
    public final Map<String, S7Connector> connectorMap = new HashMap<>();

    /**
     * 获取当前节点 DB5409
     * @param node
     * @return
     */
    public DB5410 getDB5410(MesDetectNode node) {
        int retry = 5;
        S7Connector connector = null;
        while (retry-- > 0) {
            try {
                connector = initConnection(node);
                S7SerializerImpl serializer = new S7SerializerImpl(connector);
                return serializer.dispense(DB5410.class, 5410, 0);
            } catch (Exception e) {
                log.error("读取DB5410异常, IP = {},第{}次失败", node.getIp(),5-retry, e);
                if (retry > 0) {
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException ex) {
                        Thread.currentThread().interrupt();
                    }
                }
            }finally {
                try {
                    if (connector != null) {
                        connector.close();
                    }
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
        return null;
    }

    /**
     * 获取尾站 DB5419 工件下线信息
     *
     * @param node
     * @return
     */
    public DB5419PLC getDB5419(MesDetectNode node) {
        int retry = 5;
        S7Connector connector = null;
        while (retry-- > 0) {
            try {
                connector = initConnection(node);
                S7SerializerImpl serializer = new S7SerializerImpl(connector);
                return serializer.dispense(DB5419PLC.class, 5419, 0);
            } catch (Exception e) {
                log.error("读取DB5419异常, IP = {},第{}次失败", node.getIp(),5-retry, e);
                if (retry > 0) {
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException ex) {
                        Thread.currentThread().interrupt();
                    }
                }
            }finally {
                try {
                    if (connector != null) {
                        connector.close();
                    }
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
        return null;
    }

    /**
     * 获取尾站 DB5416
     *
     * @param node
     * @return
     */
    public DB5416PLC getDB5416(MesDetectNode node) {
        int retry = 5;
        S7Connector connector = null;
        while (retry-- > 0) {
            try {
                connector = initConnection(node);
                S7SerializerImpl serializer = new S7SerializerImpl(connector);
                return serializer.dispense(DB5416PLC.class, 5416, 0);
            } catch (Exception e) {
                log.error("读取DB5416异常, IP = {},第{}次失败", node.getIp(),5-retry, e);
                if (retry > 0) {
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException ex) {
                        Thread.currentThread().interrupt();
                    }
                }
            }finally {
                try {
                    if (connector != null) {
                        connector.close();
                    }
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
        return null;
    }

    /**
     * 写入尾站 DB5416
     *
     * @param node
     * @return
     */
    public void writeDB5416(DB5416PLC db5416PLC, MesDetectNode node) {
        S7Connector connector = null;
        int retry = 5;
        while (retry-- > 0) {
            try {
                connector = initConnection(node);
                S7SerializerImpl serializer = new S7SerializerImpl(connector);
                serializer.store(db5416PLC, 5416, 150);
                return;
            } catch (Exception e) {
                log.error("写入DB5416异常, IP = {},第{}次失败", node.getIp(),5-retry, e);
            } finally {
                try {
                    connector.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }

    /**
     * 写入 DB5411
     *
     * @param node
     * @return
     */
    public void writeDB5411(DB5411 db5411, MesDetectNode node) {
        S7Connector connector = null;
        int retry = 5;
        while (retry-- > 0) {
            try {
                connector = initConnection(node);
                S7SerializerImpl serializer = new S7SerializerImpl(connector);
                serializer.store(db5411, 5411, 0);
                return;
            } catch (Exception e) {
                log.error("写入DB5411异常, IP = {},第{}次失败", node.getIp(),5-retry, e);
            } finally {
                try {
                    connector.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }

    /**
     * 写入尾站 DB5419
     *
     * @param node
     * @return
     */
    public void writeDB5419(DB5419PLC db5419PLC, MesDetectNode node) {
        S7Connector connector = null;
        int retry = 5;
        while (retry-- > 0) {
            try {
                connector = initConnection(node);
                S7SerializerImpl serializer = new S7SerializerImpl(connector);
                serializer.store(db5419PLC, 5419, 150);
                return;
            } catch (Exception e) {
                log.error("写入DB5419异常, IP = {},第{}次失败", node.getIp(),5-retry, e);
            } finally {
                try {
                    connector.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }

    /**
     * 获取当前节点 DB5409
     *
     * @param node
     * @return
     */
    public DB5409 getDB5409(MesDetectNode node) {
        int retry = 5;
        S7Connector connector = null;
        while (retry-- > 0) {
            try {
                connector = initConnection(node);
                S7SerializerImpl serializer = new S7SerializerImpl(connector);
                return serializer.dispense(DB5409.class, 5409, 0);
            } catch (Exception e) {
                if (retry > 0) {
                    try {
                        Thread.sleep(95);
                    } catch (InterruptedException ex) {
                        Thread.currentThread().interrupt();
                    }
                }
                log.error("读取DB5409异常, IP = {},第{}次失败", node.getIp(),5-retry, e);
            }finally {
                try {
                    if (connector != null) {
                        connector.close();
                    }
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
        return null;
    }

    /**
     * 获取当前节点 DB5404
     *
     * @param node
     * @return
     */
    public DB5404 getDB5404(MesDetectNode node) {
        int retry = 5;
        S7Connector connector = null;
        while (retry-- > 0) {
            try {
                connector = initConnection(node);
                S7SerializerImpl serializer = new S7SerializerImpl(connector);
                return serializer.dispense(DB5404.class, 5404, 0);
            } catch (Exception e) {
                if (retry > 0) {
                    try {
                        Thread.sleep(95);
                    } catch (InterruptedException ex) {
                        Thread.currentThread().interrupt();
                    }
                }
                log.error("读取DB5409异常, IP = {},第{}次失败", node.getIp(),5-retry, e);
            }finally {
                try {
                    if (connector != null) {
                        connector.close();
                    }
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
        return null;
    }

    /**
     * 获取当前节点 DB5409
     *
     * @param node
     * @return
     */
    public DB5850 getDB5850(MesDetectNode node) {
        int retry = 5;
        S7Connector connector = null;
        while (retry-- > 0) {
            try {
                connector = initConnection(node);
                S7SerializerImpl serializer = new S7SerializerImpl(connector);
                return serializer.dispense(DB5850.class, 5850, 0);
            } catch (Exception e) {
                log.error("读取DB5850异常, IP = {},第{}次失败", node.getIp(),5-retry, e);
                if (retry > 0) {
                    try {
                        Thread.sleep(95);
                    } catch (InterruptedException ex) {
                        Thread.currentThread().interrupt();
                    }
                }
            }finally {
                try {
                    if (connector != null) {
                        connector.close();
                    }
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
        return null;
    }
    /**
     * 获取当前节点 DB5450 中的物料编码
     * @param node
     * @return
     */
    public DB5450 getDB5450(MesDetectNode node) {
        int retry = 5;
        S7Connector connector = null;
        while (retry-- > 0) {
            try {
                connector = initConnection(node);
                S7SerializerImpl serializer = new S7SerializerImpl(connector);
                return serializer.dispense(DB5450.class, 5450, 0);
            } catch (Exception e) {
                log.error("读取DB5450异常, IP = {},第{}次失败", node.getIp(),5-retry, e);
                if (retry > 0) {
                    try {
                        Thread.sleep(95);
                    } catch (InterruptedException ex) {
                        Thread.currentThread().interrupt();
                    }
                }
            }finally {
                try {
                    if (connector != null) {
                        connector.close();
                    }
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
        return null;
    }

    /**
     * 获取当前节点 DB5405 中的生产结果
     * @param node
     * @return
     */
    public DB5405 getDB5405(MesDetectNode node) {
        int retry = 5;
        S7Connector connector = null;
        while (retry-- > 0) {
            try {
                connector = initConnection(node);
                S7SerializerImpl serializer = new S7SerializerImpl(connector);
                return serializer.dispense(DB5405.class, 5405, 0);
            } catch (Exception e) {
                log.error("读取DB5405异常, IP = {},第{}次失败", node.getIp(),5-retry, e);
                if (retry > 0) {
                    try {
                        Thread.sleep(95);
                    } catch (InterruptedException ex) {
                        Thread.currentThread().interrupt();
                    }
                }
            }finally {
                try {
                    if (connector != null) {
                        connector.close();
                    }
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
        return null;
    }


    /**
     * 获取当前节点 DB5406 中的生产结果
     * @param node
     * @return
     */
    public DB5406 getDB5406(MesDetectNode node) {
        int retry = 5;
        S7Connector connector = null;
        while (retry-- > 0) {
            try {
                connector = initConnection(node);
                S7SerializerImpl serializer = new S7SerializerImpl(connector);
                return serializer.dispense(DB5406.class, 5406, 0);
            } catch (Exception e) {
                log.error("读取DB5406异常, IP = {},第{}次失败", node.getIp(),5-retry, e);
                if (retry > 0) {
                    try {
                        Thread.sleep(95);
                    } catch (InterruptedException ex) {
                        Thread.currentThread().interrupt();
                    }
                }
            }finally {
                try {
                    if (connector != null) {
                        connector.close();
                    }
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
        return null;
    }


    /**
     * 获取当前节点 DB5407 中的生产结果
     * @param node
     * @return
     */
    public DB5407 getDB5407(MesDetectNode node) {
        int retry = 5;
        S7Connector connector = null;
        while (retry-- > 0) {
            try {
                connector = initConnection(node);
                S7SerializerImpl serializer = new S7SerializerImpl(connector);
                return serializer.dispense(DB5407.class, 5407, 0);
            } catch (Exception e) {
                log.error("读取DB5407异常, IP = {},第{}次失败", node.getIp(),5-retry, e);
                if (retry > 0) {
                    try {
                        Thread.sleep(95);
                    } catch (InterruptedException ex) {
                        Thread.currentThread().interrupt();
                    }
                }
            }finally {
                try {
                    if (connector != null) {
                        connector.close();
                    }
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
        return null;
    }



    /**
     * 获取当前节点 DB5408 中的生产结果
     * @param node
     * @return
     */
    public DB5408 getDB5408(MesDetectNode node) {
        int retry = 5;
        S7Connector connector = null;
        while (retry-- > 0) {
            try {
                connector = initConnection(node);
                S7SerializerImpl serializer = new S7SerializerImpl(connector);
                return serializer.dispense(DB5408.class, 5408, 0);
            } catch (Exception e) {
                log.error("读取DB5408异常, IP = {},第{}次失败", node.getIp(),5-retry, e);
                if (retry > 0) {
                    try {
                        Thread.sleep(95);
                    } catch (InterruptedException ex) {
                        Thread.currentThread().interrupt();
                    }
                }
            }finally {
                try {
                    if (connector != null) {
                        connector.close();
                    }
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
        return null;
    }



    /**
     * 写入尾站 DB5419
     *
     * @param node
     * @return
     */
    public void writeDB5450(DB5450 db5450, MesDetectNode node) {
        S7Connector connector = null;
        int retry = 5;
        while (retry-- > 0) {
            try {
                connector = initConnection(node);
                S7SerializerImpl serializer = new S7SerializerImpl(connector);
                serializer.store(db5450, 5450, 0);
                return;
            } catch (Exception e) {
                log.error("写入DB5450异常, IP = {},第{}次失败", node.getIp(),5-retry, e);
            } finally {
                try {
                    connector.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
    /**
     * 写入 DB5401
     *
     * @param node
     * @return
     */
    public void writeDB5401(DB5401 db5401, MesDetectNode node) {
        S7Connector connector = null;
        int retry = 5;
        while (retry-- > 0) {
            try {
                connector = initConnection(node);
                S7SerializerImpl serializer = new S7SerializerImpl(connector);
                serializer.store(db5401, 5401, 0);
                return;
            } catch (Exception e) {
                log.error("写入DB5401异常, IP = {},第{}次失败", node.getIp(),5-retry, e);
            } finally {
                try {
                    connector.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }

    /**
     * 写入尾站 DB5400
     *
     * @param node
     * @return
     */
    public void writeDB5400(DB5400 db5400, MesDetectNode node) {
        S7Connector connector = null;
        int retry = 5;
        while (retry-- > 0) {
            try {
                connector = initConnection(node);
                S7SerializerImpl serializer = new S7SerializerImpl(connector);
                serializer.store(db5400, 5400, 0);
                return;
            } catch (Exception e) {
                log.error("写入DB5450异常, IP = {},第{}次失败", node.getIp(),5-retry, e);
            } finally {
                try {
                    connector.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
    /**
     * 获取当前节点 DB5450 中的物料编码
     * @param node
     * @return
     */
    public DB5452 getDB5452(MesDetectNode node) {
        int retry = 5;
        S7Connector connector = null;
        while (retry-- > 0) {
            try {
                connector = initConnection(node);
                S7SerializerImpl serializer = new S7SerializerImpl(connector);
                return serializer.dispense(DB5452.class, 5452, 0);
            } catch (Exception e) {
                log.error("读取DB5452异常, IP = {},第{}次失败", node.getIp(),5-retry, e);
                if (retry > 0) {
                    try {
                        Thread.sleep(95);
                    } catch (InterruptedException ex) {
                        Thread.currentThread().interrupt();
                    }
                }
            }finally {
                try {
                    if (connector != null) {
                        connector.close();
                    }
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
        return null;
    }

    /**
     * 获取当前节点 DB5460PLC 中的指令
     * @param node
     * @return
     */
    public DB5460PLC getDB5460(MesDetectNode node) {
        int retry = 5;
        S7Connector connector = null;
        while (retry-- > 0) {
            try {
                connector = initConnection(node);
                S7SerializerImpl serializer = new S7SerializerImpl(connector);
                return serializer.dispense(DB5460PLC.class, 5460, 0);
            } catch (Exception e) {
                log.error("读取DB5452异常, IP = {},第{}次失败", node.getIp(),5-retry, e);
                if (retry > 0) {
                    try {
                        Thread.sleep(95);
                    } catch (InterruptedException ex) {
                        Thread.currentThread().interrupt();
                    }
                }
            }finally {
                try {
                    if (connector != null) {
                        connector.close();
                    }
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
        return null;
    }



    /**
     * 写入尾站 DB5452
     *
     * @param node
     * @return
     */
    public void writeDB5452(DB5452 db5452, MesDetectNode node) {
        S7Connector connector = null;
        int retry = 5;
        while (retry-- > 0) {
            try {
                connector = initConnection(node);
                S7SerializerImpl serializer = new S7SerializerImpl(connector);
                serializer.store(db5452, 5452, 0);
                return;
            } catch (Exception e) {
                log.error("写入DB5419异常, IP = {},第{}次失败", node.getIp(),5-retry, e);
            } finally {
                try {
                    connector.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
    /**
     * 写入追溯码查询结果 DB5463
     *
     * @param node
     * @return
     */
    public void writeDB5463(DB5463 db5463, MesDetectNode node) {
        S7Connector connector = null;
        int retry = 5;
        while (retry-- > 0) {
            try {
                connector = initConnection(node);
                S7SerializerImpl serializer = new S7SerializerImpl(connector);
                serializer.store(db5463, 5463, 0);
                return;
            } catch (Exception e) {
                log.error("写入DB5463异常, IP = {},第{}次失败", node.getIp(),5-retry, e);
            } finally {
                try {
                    connector.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
    /**
     * 写入尾站 DB5460
     *
     * @param node
     * @return
     */
    public void writeDB5460(DB5460PLC db5460, MesDetectNode node) {
        S7Connector connector = null;
        int retry = 5;
        while (retry-- > 0) {
            try {
                connector = initConnection(node);
                S7SerializerImpl serializer = new S7SerializerImpl(connector);
                serializer.store(db5460, 5460, 0);
                return;
            } catch (Exception e) {
                log.error("写入DB5460异常, IP = {},第{}次失败", node.getIp(),5-retry, e);
            } finally {
                try {
                    connector.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
    /**
     * 获取当前节点 DB5850
     *
     * @param node
     * @return
     */
    public Object getOperationCmd(MesDetectNode node) {
        int retry = 5;
        S7Connector connector = null;
        // 拼接完整类名
        String fullClassName = "com.ruoyi.order.station.operate.cmd." + node.getMachineCode();
        Class<?> clazz = null;
        try {
            clazz = Class.forName(fullClassName);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            return null;
        }
        while (retry-- > 0) {
            try {
                connector = initConnection(node);
                S7SerializerImpl serializer = new S7SerializerImpl(connector);
                return serializer.dispense(clazz, 5850, 0);
            } catch (Exception e) {
                log.error("读取DB5850异常, IP = {},第{}次失败", node.getIp(),5-retry, e);
                if (retry > 0) {
                    try {
                        Thread.sleep(95);
                    } catch (InterruptedException ex) {
                        Thread.currentThread().interrupt();
                    }
                }
            }finally {
                try {
                    if (connector != null) {
                        connector.close();
                    }
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
        return null;
    }

    /**
     * 获取当前节点 DB5850
     *
     * @param node
     * @return
     */
    public Object getOrdInfo(MesDetectNode node) {
        int retry = 5;
        S7Connector connector = null;
        // 拼接完整类名
        String fullClassName = "com.ruoyi.order.station.operate.cmd." + node.getMachineCode()+"Extend";
        Class<?> clazz = null;
        try {
            clazz = Class.forName(fullClassName);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            return null;
        }
        while (retry-- > 0) {
            try {
                connector = initConnection(node);
                S7SerializerImpl serializer = new S7SerializerImpl(connector);
                return serializer.dispense(clazz, 5852, 0);
            } catch (Exception e) {
                log.error("读取DB5852异常, IP = {},第{}次失败", node.getIp(),5-retry, e);
                if (retry > 0) {
                    try {
                        Thread.sleep(95);
                    } catch (InterruptedException ex) {
                        Thread.currentThread().interrupt();
                    }
                }
            }finally {
                try {
                    if (connector != null) {
                        connector.close();
                    }
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
        return null;
    }

    /**
     * 读取当前节点操作数据
     *
     * @param node
     * @return
     */
    public Object readOperationData(MesDetectNode node) {
        // 拼接完整类名
        String fullClassName = "com.ruoyi.order.station.operate.instance." + node.getMachineCode();
        S7Connector connector = null;
        Class<?> clazz = null;
        try {
            clazz = Class.forName(fullClassName);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            return null;
        }
        int retry = 5;
        while (retry-- > 0) {
            try {
                connector = initConnection(node);
                S7SerializerImpl serializer = new S7SerializerImpl(connector);
                int dbNum = 5851;
                return FloatPrecisionProcessor.process(serializer.dispense(clazz, dbNum, 0));
            } catch (S7Exception e) {
                log.error("读取操作数据异常,IP = {},第{}次失败", node.getIp(),5-retry, e);
                if (retry > 0) {
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException ex) {
                        throw new RuntimeException(ex);
                    }
                }
            }finally {
                if (connector!=null){
                    try {
                        connector.close();
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        }
        return null;
    }

    /**
     * 读取当前节点追溯码数据
     *
     * @param node
     * @return
     */
    public Object readQRCodeData(MesDetectNode node) {
        // 拼接完整类名
        String fullClassName = "com.ruoyi.order.station.qrcode.instance." + node.getMachineCode()+"QRCode";
        S7Connector connector = null;
        Class<?> clazz = null;
        try {
            clazz = Class.forName(fullClassName);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            return null;
        }
        int retry = 5;
        while (retry-- > 0) {
            try {
                connector = initConnection(node);
                S7SerializerImpl serializer = new S7SerializerImpl(connector);
                int dbNum = 5462;
                return FloatPrecisionProcessor.process(serializer.dispense(clazz, dbNum, 0));
            } catch (S7Exception e) {
                log.error("读取操作数据异常,IP = {},第{}次失败", node.getIp(),5-retry, e);
                if (retry > 0) {
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException ex) {
                        throw new RuntimeException(ex);
                    }
                }
            }finally {
                if (connector!=null){
                    try {
                        connector.close();
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        }
        return null;
    }

    /**
     * 读取过站请求
     *
     * @param node
     * @return
     */
    public PassRequest getPassRequest(MesDetectNode node) {
        int retry = 5;
        S7Connector connector = null;
        while (retry-- > 0) {
            try{
                connector = initConnection(node);
                S7SerializerImpl serializer = new S7SerializerImpl(connector);
                return serializer.dispense(PassRequest.class, 5411, 0);
            } catch (Exception e) {
                log.error("读取DB5411异常, IP = {},第{}次失败", node.getIp(),5-retry, e);
                if (retry > 0) {
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException ex) {
                        Thread.currentThread().interrupt();
                    }
                }
            } finally {
                try {
                    if (connector != null) {
                        connector.close();
                    }
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
        return null;
    }

    public void writePassResponse(PassResponse passResponse, MesDetectNode node) {
        S7Connector connector = null;
        int retry = 5;
        while (retry-- > 0) {
            try {
                connector = initConnection(node);
                S7SerializerImpl serializer = new S7SerializerImpl(connector);
                serializer.store(passResponse, 5411, 150);
                return;
            } catch (Exception e) {
                log.error("写入过站信息passResponse异常, IP = {},第{}次失败", node.getIp(),5-retry, e);
            } finally {
                try {
                    connector.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }

    /**
     * 写入DB5410Server
     *
     * @param db5410Server
     */
    public void wirteDB5410Server(DB5410Server db5410Server, MesDetectNode node) {
        S7Connector connector = null;
        int retry = 5;
        while (retry-- > 0) {
            try {
                connector = initConnection(node);
                S7SerializerImpl serializer = new S7SerializerImpl(connector);
                serializer.store(db5410Server, 5410, 150);
                return;
            } catch (Exception e) {
                log.error("写入DB5410异常,IP = {},第{}次失败", node.getIp(),5-retry, e);
            } finally {
                try {
                    connector.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }

    /**
     * 接收完生产数据后写入DB5439Server
     *
     * @param db5439Server
     */
    public void wirteProductDataResponse(DB5439Server db5439Server, MesDetectNode node, boolean isFirstStation) {
        int dbNum = 5439;
        if (isFirstStation) {
            dbNum = 5449;
        }
        S7Connector connector = null;
        int retry = 5;
        while (retry-- > 0) {
            try {
                connector = initConnection(node);
                S7SerializerImpl serializer = new S7SerializerImpl(connector);
                serializer.store(db5439Server, dbNum, 0);
                return;
            } catch (Exception e) {
                log.error("写入DB{}异常,IP = {},第{}次失败", dbNum,node.getIp(),5-retry, e);
            } finally {
                try {
                    connector.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }

    /**
     * 读取生产数据
     *
     * @param node
     * @return
     */
    public Object readProductData(MesDetectNode node, boolean isFirstStation) {
        String fullClassName = "com.ruoyi.order.station.request.instance." + node.getMachineCode();
        if (isFirstStation) {
            fullClassName += "DataFirst";
        } else {
            fullClassName += "Data";
        }
        int retry = 3;
        S7Connector connector = null;
        while (retry-- > 0) {
            try {
                connector = initConnection(node);
                Class<?> clazz = Class.forName(fullClassName);
                S7SerializerImpl serializer = new S7SerializerImpl(connector);
                int dbNum = isFirstStation ? 5441 : 5431;
                return FloatPrecisionProcessor.process(serializer.dispense(clazz, dbNum, 0));
            } catch (Exception e) {
                log.error("读取生产数据异常, IP = {},第{}次失败", node.getIp(),3-retry, e);
                if (retry > 0) {
                    try {
                        Thread.sleep(200);
                    } catch (InterruptedException ex) {
                        Thread.currentThread().interrupt();
                    }
                }
            }finally {
                try {
                    if (connector != null) {
                        connector.close();
                    }
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
        return null;
    }

    /**
     * 读取生产数据,扩展字段
     *
     * @param node
     * @return
     */
    public Object readProductDataExtended(MesDetectNode node) {
        String fullClassName = "com.ruoyi.order.station.request.instance." + node.getMachineCode() + "Data_1";
        int retry = 5;
        S7Connector connector = null;
        while (retry-- > 0) {
            try {
                connector = initConnection(node);
                Class<?> clazz = Class.forName(fullClassName);
                S7SerializerImpl serializer = new S7SerializerImpl(connector);
                int dbNum = 5432;
                return FloatPrecisionProcessor.process(serializer.dispense(clazz, dbNum, 0));            } catch (Exception e) {
                log.error("读取生产数据扩展字段异常, IP = {},第{}次失败", node.getIp(),5-retry, e);
                if (retry > 0) {
                    try {
                        Thread.sleep(200);
                    } catch (InterruptedException ex) {
                        Thread.currentThread().interrupt();
                    }
                }
            }finally {
                try {
                    if (connector != null) {
                        connector.close();
                    }
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
        return null;
    }

    /**
     * 读取生产数据报文头
     *
     * @param node
     * @param isFirstStation
     * @return
     */
    public ProcessRequest readProductHeader(MesDetectNode node, boolean isFirstStation) {
        int retry = 5;
        S7Connector connector = null;
        while (retry-- > 0) {
            try {
                connector = initConnection(node);
                S7SerializerImpl serializer = new S7SerializerImpl(connector);
                int dbNum = isFirstStation ? 5440 : 5430;
                return serializer.dispense(ProcessRequest.class, dbNum, 0);
            } catch (Exception e) {
                log.error("读取生产数据报文头异常, IP = {},第{}次失败", node.getIp(),5-retry, e);
                if (retry > 0) {
                    try {
                        Thread.sleep(200);
                    } catch (InterruptedException ex) {
                        Thread.currentThread().interrupt();
                    }
                }
            }finally {
                try {
                    if (connector != null) {
                        connector.close();
                    }
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
        return null;
    }

    /**
     * 初始化连接
     *
     * @return
     */
    public S7Connector initConnection(MesDetectNode node) {

        S7Connector connector = null;
  /*      if (!connectorMap.containsKey(node.getNodeCode())){
            log.info("初始化连接：节点编码：{},IP:{}", node.getNodeCode(), node.getIp());
            connector = S7ConnectorFactory.buildTCPConnector()
                    .withHost(node.getIp())
                    .withRack(0)
                    .withSlot(1)
                    .build();

        }else {
            connector = connectorMap.get(node.getNodeCode());
        }*/
        try {
            connector = S7ConnectorFactory.buildTCPConnector()
                    .withHost(node.getIp())
                    .withRack(0)
                    .withSlot(1)
                    .build();
        } catch (Exception e) {
            e.printStackTrace();
            log.error("获取plc连接失败 ip = {}, machineCode={}", node.getIp(), node.getMachineCode());
        }
        return connector;
    }

    /**
     * 释放连接
     *
     * @param node
     */
  /*  public void releaseConnection(MesDetectNode node) {
        if (connectorMap.containsKey(node.getNodeCode())) {
            S7Connector connector = connectorMap.get(node.getNodeCode());
            try {
                connector.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            connectorMap.remove(node.getNodeCode());
        }
    }*/
}